﻿// (c) Copyright Max Kazarin.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

/// <reference path='../Common/jquery-1.3.2.js' />
// <reference path='../Common/MicrosoftAjax.debug.js' />

Type.registerNamespace("jQueryNET.Data");

jQueryNET.Data.WebServiceDataSource = function jQueryNET$Data$WebServiceDataSource() {
    /// <summary>
    /// This class implement ComboBox control
    /// </summary>
    /// <param name='element' type='Sys.UI.DomElement' domElement='true'>
    /// DOM element the control is associated with
    /// </param>

    jQueryNET.Data.WebServiceDataSource.initializeBase(this);

    this._succeededQueue = new Array();
    this._failedQueue = new Array();

}

function jQueryNET$Data$WebServiceDataSource$initialize() {
	/// <summary>
	/// Initialize the control
	/// </summary>

	this._onSucceededHandler = Function.createDelegate(this, this._onSucceeded);
	this._onFailedHandler = Function.createDelegate(this, this._onFailed);
	
	//this.set_timeout(Sys.Net.WebRequestManager.get_defaultTimeout());
	
	jQueryNET.Data.WebServiceDataSource.callBaseMethod(this, "initialize");
}

function jQueryNET$Data$WebServiceDataSource$dispose() {
	/// <summary>
	/// Dispose the control
	/// </summary>

	jQueryNET.Data.WebServiceDataSource.callBaseMethod(this, "dispose");
}

function jQueryNET$Data$WebServiceDataSource$get_serviceUrl() {
	/// <value type='String'>
	/// Service url
	/// </value>

	if (arguments.length !== 0) throw Error.parameterCount();
	return this._serviceUrl;
}

function jQueryNET$Data$WebServiceDataSource$set_serviceUrl(value) {
	var e = Function._validateParams(arguments, [{ name: 'value', type: String, mayBeNull: false}]);
	if (e) throw e;
	this._serviceUrl = value;
}

function jQueryNET$Data$WebServiceDataSource$get_serviceMethod() {
	/// <value type='String'>
	/// Service method
	/// </value>

	if (arguments.length !== 0) throw Error.parameterCount();
	return this._serviceMethod;
}

function jQueryNET$Data$WebServiceDataSource$set_serviceMethod(value) {
	var e = Function._validateParams(arguments, [{ name: 'value', type: String, mayBeNull: false}]);
	if (e) throw e;
	this._serviceMethod = value;
}

function jQueryNET$Data$WebServiceDataSource$get_timeout() {
	/// <value type='String'>
	/// Web service request timeout
	/// </value>

	if (arguments.length !== 0) throw Error.parameterCount();
	return this._timeout;
}

function jQueryNET$Data$WebServiceDataSource$set_timeout(value) {
	var e = Function._validateParams(arguments, [{ name: 'value', type: Number, mayBeNull: false}]);
	if (e) throw e;
	this._timeout = value;
}

function jQueryNET$Data$WebServiceDataSource$get_parameters() {
	/// <value type='String'>
	/// Web service request timeout
	/// </value>

	if (arguments.length !== 0) throw Error.parameterCount();
	return this._parametes;
}

function jQueryNET$Data$WebServiceDataSource$set_parameters(value) {
	var e = Function._validateParams(arguments, [{ name: 'value', type: Object, mayBeNull: false}]);
	if (e) throw e;
	this._isLoaded = false;
	this._data = null;
	this._parametes = value;
}

function jQueryNET$Data$WebServiceDataSource$get_isLoading() {
	if (arguments.length !== 0) throw Error.parameterCount();
	return this._isLoading;
}

function jQueryNET$Data$WebServiceDataSource$get_isLoaded() {
	if (arguments.length !== 0) throw Error.parameterCount();
	return this._isLoaded;
}

function jQueryNET$Data$WebServiceDataSource$clear() {
	this._isLoaded = false;
	this._data = null;
}

function jQueryNET$Data$WebServiceDataSource$fetch(parameters, onSucceeded, onFailed) {
	if (this.get_isLoaded()) {
		onSucceeded(this, this._data);
		return;
	}

	this._succeededQueue.push(onSucceeded);
	this._failedQueue.push(onFailed);

	if (this.get_isLoading())
		return;
	
	this._isLoading = true;
	this._data = null;
	this._isLoaded = false;

	var params = apply(parameters, this.get_parameters());
	Sys.Net.WebServiceProxy.invoke(
		this.get_serviceUrl(),
		this.get_serviceMethod(),
		false,
		params,
		this._onSucceededHandler,
		this._onFailedHandler,
		null,
		this.get_timeout());
}

function jQueryNET$Data$WebServiceDataSource$_onSucceeded(result, userContext, methodName) {
	this._isLoading = false;
	this._isLoaded = true;
	this._data = result;

	var onSucceeded = null;
	while (onSucceeded = this._succeededQueue.shift())
		onSucceeded(this, result);
	
	//this.raiseSucceeded(result);
}

function jQueryNET$Data$WebServiceDataSource$_onFailed(result, userContext, methodName) {
	this._isLoading = false;
	this._data = null;
	var onFailed = null;
	while (onFailed = this._failedQueue.shift())
		onFailed(this, result);

	//this.raiseFailed(result);
}

jQueryNET.Data.WebServiceDataSource.prototype = {
	_serviceUrl: null,
	_serviceMethod: null,
	_timeout: 0, 
	_onSucceededHandler: null,
	_onFailedHandler: null,
	_parametes: null,
	_isLoading: false,
	_isLoaded: false,
	_succeededQueue: null,
	_failedQueue: null,
	
	initialize: jQueryNET$Data$WebServiceDataSource$initialize,
	dispose: jQueryNET$Data$WebServiceDataSource$dispose,

	get_serviceUrl: jQueryNET$Data$WebServiceDataSource$get_serviceUrl,
	set_serviceUrl: jQueryNET$Data$WebServiceDataSource$set_serviceUrl,
	get_serviceMethod: jQueryNET$Data$WebServiceDataSource$get_serviceMethod,
	set_serviceMethod: jQueryNET$Data$WebServiceDataSource$set_serviceMethod,
	get_timeout: jQueryNET$Data$WebServiceDataSource$get_timeout,
	set_timeout: jQueryNET$Data$WebServiceDataSource$set_timeout,
	get_parameters: jQueryNET$Data$WebServiceDataSource$get_parameters,
	set_parameters: jQueryNET$Data$WebServiceDataSource$set_parameters,
	get_isLoading: jQueryNET$Data$WebServiceDataSource$get_isLoading,
	get_isLoaded: jQueryNET$Data$WebServiceDataSource$get_isLoaded,

	clear: jQueryNET$Data$WebServiceDataSource$clear,
	fetch: jQueryNET$Data$WebServiceDataSource$fetch,
	
	_onSucceeded: jQueryNET$Data$WebServiceDataSource$_onSucceeded,
	_onFailed: jQueryNET$Data$WebServiceDataSource$_onFailed
}

jQueryNET.Data.WebServiceDataSource.registerClass("jQueryNET.Data.WebServiceDataSource", jQueryNET.Data.DataSourceBase);